<?php

namespace api\modules\v1\controllers\base;

use common\helpers\CommonHelper;
use services\linkedcare\LinkedcareTagsService;
use services\base\TreatmentProjectService;
use Yii;
use api\controllers\OnAuthController;
use yii\web\HttpException;

class TreatmentProjectController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/base/treatment-project/index 业务项目列表
     * @apiGroup TreatmentProject
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {string} keywords
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list             列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {Number}  data.list.status           状态
     * @apiSuccess {String}  data.list.status_name      状态名称
     * @apiSuccess {String}  data.list.tag_name         E看牙项目名
     * @apiSuccess {Number}  data.list.sort             排序
     * @apiSuccess {Number}  data.condition             E看牙标签
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $commonService = new TreatmentProjectService();
        $list = $commonService->treatmentProjectList($params, $page, $pageSize);
        $condition = LinkedcareTagsService::getTagsCondition();
        return [
            'list' => $list,
            'condition' => $condition,
        ];
    }

    /**
     * @throws \Exception
     * @api {post} /v1/base/treatment-project/update-treatment-project 更新业务项目
     * @apiGroup TreatmentProject
     * @apiName update-treatment-project
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                   ID【编辑时必传】
     * @apiBody {String}   name                 名称
     * @apiBody {int}      tag_id                E看牙项目ID
     * @apiBody {Number}   sort                 排序
     * @apiBody {Array}    status               状态【0：禁用; 1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdateTreatmentProject()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '名称必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $commonService = new TreatmentProjectService();
        return $commonService->updateTreatmentProject($params);
    }

    /**
     * @api {post} /v1/base/treatment-project/delete-treatment-project 删除业务项目（软删除）
     * @apiGroup TreatmentProject
     * @apiName delete-treatment-project
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteTreatmentProject()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $commonService = new TreatmentProjectService();
        return $commonService->deleteTreatmentProject($params);
    }
}
